<template>
    <div>
        <com-alert
        :show.sync="alertData.show"
        :title.sync="alertData.title"
        :buttontxt.sync="alertData.buttontxt"
        :content.sync="alertData.content"
        ></com-alert>

        <confirm
        :show.sync="firmData.show"
        :confirm-text.sync='firmData.confirmText'
        :cancel-text.sync='firmData.cancelText'
        @on-confirm=" setContract()"
        >

            <div class="contract_show" v-if=" firmData.content == 'permit' ">
                <div class="c_note">
                    <!-- 你还没有 <b style="color:#ffae44">自动签章授权</b> 给有喜财富<br>没法进行投资!-->
                    为保障您的合法权益，请授权 <b style="color:#ffae44">法大大电子签章。</b>
                </div>
                <div class="img">
                    <img src="../../assets/images/user/contract_logo.png">
                </div>
            </div>

            <div class="contract_show" v-if=" firmData.content == 'sign' ">
                <div class="c_note">你还没有签约 <b style="color:#ffae44">法大大签章</b>, 没法进行投资!</div>
                <div class="img"> <img src="../../assets/images/user/contract_logo.png"></div>
                <div><a href="javascript:void(0);" style="color:#333" @click="onShowTerms()" > 了解更多>> </a></div>
            </div>
        </confirm>
    </div>
</template>

<script>
import ComAlert from './alert'
import {Confirm} from 'vux'
import { mapGetters } from 'vuex'
import api from '../../data/api';
var vm = this;
export default {

    data(){
        return{

            alertData:{
                show:false,
                title:'标题',
                buttontxt:'确定',
                content:'sign'
			},

            firmData:{
                show:false,
                confirmText:'确认',
                cancelText:'再看看',
                content:''
			},

			show_terms:false
        }
    },

    computed: {
		...mapGetters([
            'contractStatus',
            'showDialog',
            'showLoading',
            'contractPermit',
            'contractSign'
        ])
    },


	components: {
        ComAlert,
        Confirm
	},

    methods:{

        onShowTerms(){
			//vm.firmData.show = false;

			console.log('why3');
            vm.alertData.show = true;
            vm.alertData.title = '关于电子签章详情';
            vm.alertData.buttontxt = '确认';
            vm.alertData.content = '<p>目前“有喜财富”已与国内领先的第三方电子合同平台法大大达成合作，引入法大大电子合同，可以全面实现在线合同的数字签名及合同托管，进一步达到了监管机构对P2P平台合规的要求，实现了借贷双方的穿透，第三方电子合同能极大地解决目前网贷交易过程中借贷双方时间、空间不一致无法一起签订传统纸质合同问题。因此需要您在通过法大大平台申请个人数字签章，并进行授权使用。</p>';

        },

        async getContract(){

			vm.$store.dispatch('setShowLoading', true);
			//var res = await api.user.get_contract_status();
            //vm.$store.dispatch('setContractStatus', res);
            await vm.$store.dispatch('getContractStatus');
			vm.$store.dispatch('setShowLoading', false);

            //vm.contractStatus.open_status = true;
			//申请申请窗口
			if(vm.contractStatus.open_status){
				console.log('why1');
				vm.firmData.content = 'sign';
				vm.firmData.confirmText = '去签章'
                vm.firmData.show = true;

                vm.$store.dispatch('setShowDialog', true);
				return; //如果还没有签章,不显示授权。
			}
			//自动签章授权窗口
			if(vm.contractStatus.auto_status){
				console.log('why2');
				vm.firmData.content = 'permit';
                vm.firmData.confirmText = '去授权'
                vm.$store.dispatch('setShowDialog', true);
				vm.firmData.show = true;
			}

		},


		//申请签章及授权
        async setContract(){
			var res = null;
			vm.$store.dispatch('setShowLoading', true);
			console.log(vm.firmData.content);

			if( vm.firmData.content ==  'sign'){

				//申请签章
                //res = await api.user.investor_sign_contract();

                await vm.$store.dispatch('setContractSign');
                res = vm.contractSign;
				vm.$store.dispatch('setShowLoading', false);
                vm.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:res.msg};

				if(res.status){
					setTimeout(function() {
						vm.alertData.show = false;
						vm.getContract();
					}, 2500);
				}

			}else{
                //申请授权
				//res = await api.user.investor_contract_permit();
				//vm.$store.dispatch('setShowLoading', false);

                //vm.$store.dispatch('setShowLoading', true);
                await vm.$store.dispatch('setContractPermit');



                if(vm.contractPermit.status){
                    window.location.href = vm.contractPermit.url;
				}else{
                    vm.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:vm.contractPermit.msg};
                }
                vm.$store.dispatch('setShowLoading', false);

			}
        }

    },

    created(){
        vm  = this;

        /* this.$nextTick(() => {
            vm.getContract();
        }); */
    }
}
</script>

 <style lang="scss">
.weui_cells{
    margin-top:0px;
    margin-bottom:10px;
}
</style>


<style lang="scss" scoped>
.contract_show { text-align: center;  }
.contract_show .c_note, .contract_show .img { margin-bottom: 10px; }
</style>
